package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.ImageModle;

public class EnGridViewAdapter extends BaseAdapter {

    private Context mContext;
    private List<ImageModle> list = new ArrayList<ImageModle>();

    public EnGridViewAdapter(Context mContext, List<ImageModle> list) {
        super();
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public int getCount() {

        return list.size();

    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_published_grida, null);
            holder = new ViewHolder();
            holder.item_grida_image = (ImageView) convertView.findViewById(R.id.item_grida_image);
            holder.imgLaout=(LinearLayout) convertView.findViewById(R.id.imgLaout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.imgLaout.setTag(position);
        try {
            Glide.with(mContext).load(list.get(position).getImages()).asBitmap().into(holder.item_grida_image);
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.imgLaout .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer)v.getTag();
                String str="";
                for(int i=0;i<list.size();i++){
                    if(i==list.size()-1){
                        str+=list.get(i).getImages();
                    }else{
                        str+=list.get(i).getImages()+",";
                    }
                }
                imageBrower(num,str);
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView item_grida_image;
        LinearLayout imgLaout;
    }
    protected void imageBrower(int position, String images) {
//        Intent intent = new Intent(mContext, ImagePagerActivity.class);
//        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
//        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, images);
//        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
//        mContext.startActivity(intent);
    }
}
